package com.jic.product.controller.manage;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.constant.consist.ResponseCode;
import com.jic.product.entity.CategoryPropertyValue;
import com.jic.product.entity.ProductPropertyName;
import com.jic.product.entity.ProductPropertyValue;
import com.jic.product.exception.AppRuntimeException;
import com.jic.product.manage.request.ProductPropertyNameEditRequest;
import com.jic.product.manage.request.ProductPropertyNameRequest;
import com.jic.product.manage.request.ProductPropertyValueRequest;
import com.jic.product.manage.response.ProductPropertyNameResponse;
import com.jic.product.service.CategoryPropertyValueService;
import com.jic.product.service.ProductPropertyNameService;
import com.jic.product.service.ProductPropertyValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ZW on 2020/1/9 11:04
 * 商品属性管理
 */
@RestController
@RequestMapping("/productNameProperty")
@Api(value = "商品设置", description = "属性管理", tags = "商品管理")
@Slf4j
public class ProductPropertyNameController {

    @Autowired
    private ProductPropertyNameService productPropertyNameService;

    @Autowired
    private ProductPropertyValueService productPropertyValueService;

    @Autowired
    private CategoryPropertyValueService categoryPropertyValueService;

    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取所有商品属性信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<ProductPropertyNameResponse>> queryPage(@RequestBody ProductPropertyNameRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        ProductPropertyName productPropertyName = BeanUtil.copy(form, ProductPropertyName.class);
        PageResult<ProductPropertyNameResponse> result = productPropertyNameService.queryPage(productPropertyName, page);
        List<ProductPropertyNameResponse> productPropertyNameResponseList = result.getRows().stream().map(temp -> {
            ProductPropertyNameResponse productPropertyNameResponse = BeanUtil.copy(temp, ProductPropertyNameResponse.class);
            return productPropertyNameResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<ProductPropertyNameResponse> pageResult = new PageResult<>();
        pageResult.setRows(productPropertyNameResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }


    /*
     *  商品属性新增
     *
     * */
    @ResponseBody
    @PostMapping(value = "/editProductProperty")
    public RestResult editProductProperty(@RequestBody ProductPropertyNameEditRequest productPropertyNameEditRequest) {
        log.info("开始进入商品属性新增==========》" + productPropertyNameEditRequest.toString());
        RestResult restResult = new RestResult<>();
        try {
            //修改
            if (productPropertyNameEditRequest.getId() != 0) {
                restResult = productPropertyNameService.update(productPropertyNameEditRequest);
            } else {
                productPropertyNameEditRequest.setDeptId(productPropertyNameEditRequest.getAdminOperate().getDeptId());
                restResult = productPropertyNameService.insert(productPropertyNameEditRequest);
            }
        } catch (Exception e) {
            log.error("editProductProperty ==========》" + e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION,e.getLocalizedMessage());
        }
        return restResult;

    }

    /*
     *  商品属性删除
     *
     * */
    @ResponseBody
    @PostMapping(value = "/deleteProductProperty")
    public RestResult deleteProductProperty(@RequestBody HashMap<String, String> map) {
        return productPropertyNameService.delete(map.get("ids"));
    }


    /*
     *  商品属性条件查询
     *
     * */
    @ResponseBody
    @PostMapping(value = "/queryProductProperty")
    public RestResult query(@RequestBody ProductPropertyNameRequest productPropertyNameRequest) {
        log.info("开始进入商品属性条件查询==========》" + productPropertyNameRequest.toString());
        RestResult restResult = new RestResult();
        List<ProductPropertyName> productPropertys = null;
        try {
            ProductPropertyName productProperty = BeanUtil.copy(productPropertyNameRequest, ProductPropertyName.class);
            productPropertys = productPropertyNameService.query(productProperty);
            restResult.setCode("0000");
            restResult.setData(productPropertys);
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            log.error("query==========》" + e);
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;
    }

    /*
     *  商品属性值条件查询
     *
     * */
    @ResponseBody
    @PostMapping(value = "/queryProductPropertyValue")
    public RestResult queryProductPropertyValue(@RequestBody ProductPropertyValueRequest productPropertyValueRequest) {
        log.info("开始进入商品属性条件查询==========》" + productPropertyValueRequest.toString());
        RestResult restResult = new RestResult();
        List<ProductPropertyValue> productPropertyValues = null;
        try {
            productPropertyValues = productPropertyNameService.queryPropertyValue(productPropertyValueRequest);
            restResult.setCode("0000");
            restResult.setData(productPropertyValues);
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            log.error("queryProductPropertyValue==========》" + e);
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;
    }

    /*
     *  根据属性值id查询是否关联类目
     *
     * */
    @ResponseBody
    @PostMapping(value = "/queryByPropertyValueId")
    public RestResult queryByPropertyValueId(@RequestBody CategoryPropertyValue categoryPropertyValue) {
        log.info("开始进入商品属性条件查询==========》" + categoryPropertyValue.toString());
        RestResult restResult = new RestResult();
        List<CategoryPropertyValue> categoryPropertyValues = null;
        try {
            categoryPropertyValues = categoryPropertyValueService.query(categoryPropertyValue);
            restResult.setCode("0000");
            restResult.setData(categoryPropertyValues);
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            log.error("queryByPropertyValueId==========》" + e);
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;
    }


    /*
     *  商品属性值条件查询
     *
     * */
    @ResponseBody
    @PostMapping(value = "/updateProductPropertyValue")
    public RestResult updateProductPropertyValue(@RequestBody ProductPropertyValueRequest productPropertyValueRequest) {
        log.info("开始进入商品属性修改==========》" + productPropertyValueRequest.toString());
        RestResult restResult = new RestResult();
        try {
            productPropertyValueService.update(BeanUtil.copy(productPropertyValueRequest, ProductPropertyValue.class));
            restResult.setCode("0000");
            restResult.setMsg("调用成功");
        } catch (Exception e) {
            log.error("updateProductPropertyValue==========》" + e);
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;
    }

}
